package top.huxy.boot.controller.admin;

import lombok.Data;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.huxy.boot.annotation.Master;
import top.huxy.boot.exception.RequestParamIllegalException;
import top.huxy.boot.service.NavSiteService;
import top.huxy.boot.util.StringUtils;
import top.huxy.boot.util.obj.Result;
import top.huxy.boot.util.obj.ValidateAble;

@RequestMapping("/admin/home")
@RestController
public class AdminHomeController {

    private NavSiteService navSiteService;

    public AdminHomeController(NavSiteService navSiteService) {
        this.navSiteService = navSiteService;
    }

    @Data
    public static class DTOSite implements ValidateAble {
        private Integer id;
        private String name;
        private String grp;
        private String logo;
        private String url;
        private Integer weight;
        private String desc;

        @Override
        public void validate() {
            String temp;
            if ((temp = StringUtils.getEmptyParamName("name,grp,logo,url", name, grp, logo, url)) != null) {
                throw new RequestParamIllegalException("param '" + temp + "' is required");
            }
        }
    }

    @Master
    @PostMapping("/addSite")
    public Result addSite(DTOSite site) {
        navSiteService.insert(site);
        return Result.success("新增成功");
    }

    @Master
    @PostMapping("/updateSiteGroup")
    public Result updateSiteGroup(DTOEditGroup dtoEditGroup) {
        navSiteService.updateSiteGroup(dtoEditGroup);
        return Result.success("修改成功");
    }

    @Master
    @PostMapping("/updateSite")
    public Result updateSite(DTOSite site) {
        navSiteService.updateSite(site);
        return Result.success("修改成功");
    }

    @Data
    public static class DTOEditGroup implements ValidateAble {
        private String rawName;
        private String newName;

        @Override
        public void validate() {
            String temp;
            if ((temp = StringUtils.getEmptyParamName("rawName,newName", rawName, newName)) != null) {
                throw new RequestParamIllegalException("param '" + temp + "' is required");
            }
        }
    }

    @Master
    @PostMapping("/moveGroup")
    public Result moveGroup(@RequestParam("grp") String grp, @RequestParam("target") String target, @RequestParam("type") String type) {
        if (grp.equals(target)) {
            throw new RequestParamIllegalException("grp must not equals target");
        }
        if (StringUtils.isEmpty(grp, target, type)) {
            throw new RequestParamIllegalException("params must not null");
        }
        if (!type.equals("before") && !type.equals("after")) {
            throw new RequestParamIllegalException("illegal type : " + type);
        }
        navSiteService.moveGroup(grp, target, type);
        return Result.success("移动成功");
    }

    @Master
    @PostMapping("/moveItem")
    public Result moveItem(@RequestParam("id") Integer id, @RequestParam(value = "target", required = false) Integer target, @RequestParam("type") String type) {
        if (StringUtils.isEmpty(id, type)) {
            throw new RequestParamIllegalException("params must not null");
        }
        if (!type.equals("before") && !type.equals("after") && !type.equals("inner")) {
            throw new RequestParamIllegalException("illegal type : " + type);
        }
        navSiteService.moveItem(id, target, type);
        return Result.success("移动成功");
    }

    @Master
    @PostMapping("/delSiteGroup")
    public Result delSiteGroup(@RequestParam("id") Integer id) {
        navSiteService.delSiteGroup(id);
        return Result.success("删除组成功");
    }

    @Master
    @PostMapping("/delSite")
    public Result delSite(@RequestParam("id") Integer id) {
        navSiteService.delSite(id);
        return Result.success("删除网站成功");
    }
}
